package cn.seecoder.ai.serviceImpl;

import cn.seecoder.ai.dao.AnswerRepository;
import cn.seecoder.ai.dao.FileFieldRepository;
import cn.seecoder.ai.dao.FileInfoRepository;
import cn.seecoder.ai.dao.PredictionRepository;
import cn.seecoder.ai.model.po.Answer;
import cn.seecoder.ai.model.po.FileField;
import cn.seecoder.ai.model.po.Prediction;
import cn.seecoder.ai.model.vo.FileInfoVO;
import cn.seecoder.ai.model.vo.ResultVO;
import cn.seecoder.ai.service.FileAnalysisService;
import cn.seecoder.ai.service.FileInfoService;
import cn.seecoder.ai.service.InstructionService;
import cn.seecoder.ai.model.vo.ConfigVO;
import cn.seecoder.ai.model.vo.ModelVO;
import cn.seecoder.ai.service.*;
import org.apache.spark.sql.Row;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author DingXiaoyu
 * @date 2023/5/8 15:03
 */
@Service
public class InstructionServiceImpl implements InstructionService {

    @Autowired
    AnswerRepository answerRepository;

    @Autowired
    PredictionRepository predictionRepository;

    @Autowired
    FileFieldRepository fileFieldRepository;

    @Autowired
    FileAnalysisService fileAnalysisService;

    @Autowired
    FileInfoRepository fileInfoRepository;

    @Autowired
    ModelService modelService;

    @Autowired
    ConfigService configService;

    @Override
    public Boolean setAnswer(Integer origin, Integer answer) {
        List<Answer> answers=answerRepository.findByOrigin(origin);
        if (answers.size()==0){
            answerRepository.save(Answer.builder().origin(origin).answer(answer).build());
        }else {
            answerRepository.deleteAllByOrigin(origin);
            answerRepository.save(Answer.builder().origin(origin).answer(answer).build());
        }
        return true;
    }

    @Override
    public Integer checkAnswer(Integer predictionId) {
        try{
            Prediction prediction=predictionRepository.findById(predictionId).get();
            ModelVO modelVO=modelService.getModelVO(prediction.getUserId(),prediction.getModelId());
            ConfigVO configVO=configService.getConfig(prediction.getUserId(),modelVO.getConfigId());
            String label=configVO.getLabel();
            Integer fileInfoId=prediction.getFileInfoId();
            if (answerRepository.findByOrigin(fileInfoId).size()==0){
                return -1;
            }
            Integer answerId=answerRepository.findByOrigin(fileInfoId).get(0).getAnswer();
            List<Row> row1=fileAnalysisService.getRows(prediction.getPredictionFileDownloadingUri(),"predicted_label");
            List<Row> row2=fileAnalysisService.getRows(fileInfoRepository.findById(answerId).get().getOriginalFileUri(),label);
            if (row1.size()!=row2.size()){
                throw new Exception();
            }
            int count=0;
            for (int i=0;i<row1.size();i++){
                Row a=row1.get(i);
                Row b=row2.get(i);
                try{
                    Integer a1=a.getInt(0);
                    Integer b1=b.getInt(0);
                    if (a1.equals(b1)){
                        count++;
                    }
                }catch (Exception e){
                    try {
                       Double a2=a.getDouble(0);
                       Double b2=b.getDouble(0);
                       if (a2.equals(b2)){
                           count++;
                       }
                    }catch (Exception ee){
                        try {
                            String a3=a.getString(0);
                            String b3=b.getString(0);
                            if (a3.equals(b3)){
                                count++;
                            }
                        }catch (Exception eee){
                            throw new Exception("无法对比");
                        }
                    }
                }
            }
            return count*100/row1.size();
        }catch (Exception e){
            e.printStackTrace();
            return 0;
        }
    }
}
